<!--
 * @Descripttion: 采购模板
 * @Author: xiao li
 * @Date: 2020-12-09 11:02:52
 * @LastEditors: xiao li
 * @LastEditTime: 2022-02-16 18:04:55
-->

<template>
  <div class="lb-goods-list">
    <top-nav :isBack="true" />
    <div class="page-main">
      <lb-tips>
        <p>
          注：用户商品下单达到相应数量时，价格自动优惠，该优惠与会员优惠、优惠券、砍价及拼团营销活动不能同时使用。会员优惠和批发价优惠同时存在时，以最低优惠下单。
        </p>
      </lb-tips>
      <lb-button
        size="medium"
        type="primary"
        opType="add"
        icon="el-icon-plus"
        @click="$router.push(`/malls/editWholesale?id=${searchForm.goods_id}`)"
        >{{ $t('menu.WholesaleAdd') }}</lb-button
      >
      <div class="space-lg"></div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="spe_name" label="商品规格名"> </el-table-column>
        <el-table-column prop="num_text" label="批发数量"> </el-table-column>
        <el-table-column prop="discount_text" label="批发折扣">
        </el-table-column>
        <el-table-column prop="address" width="180px" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  $router.push(
                    `/malls/editWholesale?id=${searchForm.goods_id}&cid=${scope.row.id}`
                  )
                "
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="toDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      id: 0,
      loading: false,
      searchForm: {
        page: 1,
        limit: 10
      },
      cateOptions: [],
      tableData: [],
      total: 0
    }
  },
  created () {
    let { id } = this.$route.query
    this.searchForm.goods_id = id
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 获取列表
     */
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { code, data } = await this.$api.wholesaleList(this.searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    /**
     * @method 上下架
     */
    updateItem (id, status) {
      this.$api.wholesaleStatusUpdate({ id, status }).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - id.length) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - id.length) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    /**
     * @method: 删除
     * @param {*} id
     */
    toDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-goods-list {
  width: 100%;
  .page-main {
    width: 100%;
    padding: 20px;
    .el-table {
      img {
        width: 80px;
        height: 80px;
      }
    }
  }
}
</style>
